package yaty.lee.tencent.weibo.modules.t;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import yaty.lee.tencent.weibo.domain.BackInfo;
import yaty.lee.tencent.weibo.domain.DelBackInfo;
import yaty.lee.tencent.weibo.domain.UploadPicBackInfo;
import yaty.lee.tencent.weibo.modules.t.vo.Add1VO;
import yaty.lee.tencent.weibo.modules.t.vo.AddEmotionVO;
import yaty.lee.tencent.weibo.modules.t.vo.AddMultiVO;
import yaty.lee.tencent.weibo.modules.t.vo.AddMusicVO;
import yaty.lee.tencent.weibo.modules.t.vo.AddPicUrlVo;
import yaty.lee.tencent.weibo.modules.t.vo.AddPicVO;
import yaty.lee.tencent.weibo.modules.t.vo.AddVO;
import yaty.lee.tencent.weibo.modules.t.vo.AddVideoVO;
import yaty.lee.tencent.weibo.modules.t.vo.CommentVO;
import yaty.lee.tencent.weibo.modules.t.vo.DelVO;
import yaty.lee.tencent.weibo.modules.t.vo.ListVO;
import yaty.lee.tencent.weibo.modules.t.vo.ReAddVO;
import yaty.lee.tencent.weibo.modules.t.vo.ReCountVO;
import yaty.lee.tencent.weibo.modules.t.vo.ReplyVO;
import yaty.lee.tencent.weibo.modules.t.vo.UploadPicVO;
import yaty.lee.tencent.weibo.util.HttpURLConnectionUtil;
import yaty.lee.tencent.weibo.util.OAuthDataUtil;
import yaty.lee.tencent.weibo.util.OAuthPropertiesUtil;
import yaty.lee.tencent.weibo.util.ReflectUtil;
import yaty.lee.tencent.weibo.util.ResponseDetectUtil;

import com.alibaba.fastjson.JSON;

public class T {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void add() {
        AddVO addVO = new AddVO(T_Constant.FORMAT_JSON, "10月最有意义的一天是……", "212.34.23.1");
        addVO.setLatitude(116.607214);
        addVO.setLongitude(40.087517);
        BackInfo backInfo = HttpURLConnectionUtil.postToObject(addVO);
        ResponseDetectUtil.detect(backInfo);
    }

    @Test
    public void add1() throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        Add1VO add1VO = new Add1VO(T_Constant.FORMAT_JSON, "11月最有意义的一天是……", "212.34.23.1");
        add1VO.setLongitude(116.607214);
        add1VO.setLatitude(40.087517);

        Map<String, Object> map = ReflectUtil.getAllField(add1VO);
        Set<String> strs = new TreeSet<String>();
        for (Entry<String, Object> entry : map.entrySet()) {
            if ("oauth_signature".equals(entry.getKey())) {
                continue;
            }
            strs.add(entry.getKey() + "=" + URLEncoder.encode(entry.getValue() + "", "UTF-8"));
        }

        String basestring = "POST&" + URLEncoder.encode(add1VO.getReqUrl(), "UTF-8") + "&" + URLEncoder.encode(ReflectUtil.join(strs, "&"), "UTF-8");
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec secret = new SecretKeySpec((OAuthPropertiesUtil.getSecret() + "&" + OAuthDataUtil.getOAuthTokenSecret()).getBytes(), mac.getAlgorithm());
        mac.init(secret);
        byte[] result = Base64.encodeBase64(mac.doFinal(basestring.getBytes()));
        add1VO.setOauth_signature(new String(result));

        BackInfo backInfo = HttpURLConnectionUtil.postToObject(add1VO);
        ResponseDetectUtil.detect(backInfo);
    }

    @Test
    public void addPic() throws IOException {
        AddPicVO addPicVO = new AddPicVO(T_Constant.FORMAT_JSON, "南国有伊人" + System.currentTimeMillis(), "212.34.23.1", "D:\\1.jpg");
        addPicVO.setLatitude(35.685954);
        addPicVO.setLongitude(139.750643);
        BackInfo backInfo = HttpURLConnectionUtil.postFileToObject(addPicVO);
        ResponseDetectUtil.detect(backInfo);
    }

    @Test
    public void addPicUrl() {
        AddPicUrlVo addPicUrlVo = new AddPicUrlVo(T_Constant.FORMAT_JSON, "漂亮美眉。", "212.34.23.10", "http://img.nr99.com/attachment/forum/201310/11/095320klelezt5el22e255.jpg");
        addPicUrlVo.setLatitude(116.607214);
        addPicUrlVo.setLongitude(40.087517);
        BackInfo backInfo = HttpURLConnectionUtil.postToObject(addPicUrlVo);
        ResponseDetectUtil.detect(backInfo);
    }

    @Test
    public void addEmotion() {
        AddEmotionVO addEmotionVO = new AddEmotionVO(T_Constant.FORMAT_JSON, "鱼肚", "212.34.23.10", 3);
        addEmotionVO.setLatitude(116.607214);
        addEmotionVO.setLongitude(40.087517);
        BackInfo backInfo = HttpURLConnectionUtil.postToObject(addEmotionVO);
        ResponseDetectUtil.detect(backInfo);
    }

    @Test
    public void addMusic() {
        AddMusicVO addMusicVO = new AddMusicVO(T_Constant.FORMAT_JSON, "喜欢这首歌", "212.34.23.10", "有一点动心", "张信哲");
        addMusicVO.setLatitude(116.607214);
        addMusicVO.setLongitude(40.087517);
        BackInfo backInfo = HttpURLConnectionUtil.postToObject(addMusicVO);
        ResponseDetectUtil.detect(backInfo);
    }

    @Test
    public void addVideo() {
        AddVideoVO addVideoVO = new AddVideoVO(T_Constant.FORMAT_JSON, "噗", "212.34.23.10", "http://v.youku.com/v_show/id_XNjE5Nzg2MDcy.html");
        addVideoVO.setLatitude(116.607214);
        addVideoVO.setLongitude(40.087517);
        BackInfo backInfo = HttpURLConnectionUtil.postToObject(addVideoVO);
        ResponseDetectUtil.detect(backInfo);
    }

    @Test
    public void addMulti() {
        AddMultiVO addMultiVO = new AddMultiVO(T_Constant.FORMAT_JSON, "你的背包", "212.34.23.10");
        addMultiVO.setLatitude(116.607214);
        addMultiVO.setLongitude(40.087517);
        addMultiVO.setPic_url("http://img3.douban.com/view/group_topic/large/public/32999591-1.jpg");
        addMultiVO.setVideo_url("http://v.youku.com/v_show/id_XMjg5NTcyMzY=.html");
        addMultiVO.setMusic_url("http://stream17.qqmusic.qq.com/30462353.mp3");
        addMultiVO.setMusic_title("你的背包");
        addMultiVO.setMusic_author("陈奕迅");
        BackInfo backInfo = HttpURLConnectionUtil.postToObject(addMultiVO);
        ResponseDetectUtil.detect(backInfo);
    }

    @Test
    public void uploadPic() {
        UploadPicVO uploadPicVO = new UploadPicVO(T_Constant.FORMAT_JSON, 1);
        uploadPicVO.setPic_url("http://img.nr99.com/attachment/forum/201310/08/151054j89i9mi2ciwdun22.jpg");
        UploadPicBackInfo backPicInfo = HttpURLConnectionUtil.upload(uploadPicVO);

        AddPicUrlVo addPicUrlVo = new AddPicUrlVo(T_Constant.FORMAT_JSON, "美眉很漂亮。", "212.34.23.10", backPicInfo.getData().getImgurl());
        addPicUrlVo.setLatitude(116.607214);
        addPicUrlVo.setLongitude(40.087517);
        BackInfo backInfo = HttpURLConnectionUtil.postToObject(addPicUrlVo);
        ResponseDetectUtil.detect(backInfo);
    }

    @Test
    public void reAdd() throws InterruptedException {
        AddVO addVO = new AddVO(T_Constant.FORMAT_JSON, "郭德纲《济公传》北展超清晰豪华全集！", "212.34.23.1");
        addVO.setLatitude(116.607214);
        addVO.setLongitude(40.087517);
        BackInfo backInfo = HttpURLConnectionUtil.postToObject(addVO);
        ResponseDetectUtil.detect(backInfo);

        Thread.sleep(15000);

        ReAddVO reAddVO = new ReAddVO(T_Constant.FORMAT_JSON, "转！", "212.34.23.1", backInfo.getData().getId());
        reAddVO.setLatitude(116.607214);
        reAddVO.setLongitude(40.087517);
        backInfo = HttpURLConnectionUtil.postToObject(reAddVO);
        ResponseDetectUtil.detect(backInfo);
    }

    @Test
    public void del() throws InterruptedException {
        AddVO addVO = new AddVO(T_Constant.FORMAT_JSON, "郭德纲《济公传》北展超清晰豪华全集！", "212.34.23.1");
        addVO.setLatitude(116.607214);
        addVO.setLongitude(40.087517);
        BackInfo backInfo = HttpURLConnectionUtil.postToObject(addVO);
        ResponseDetectUtil.detect(backInfo);

        Thread.sleep(15000);

        DelVO delVO = new DelVO(T_Constant.FORMAT_JSON, backInfo.getData().getId());
        DelBackInfo delBackInfo = JSON.parseObject(HttpURLConnectionUtil.post(delVO), DelBackInfo.class);
        System.out.println(delBackInfo.getData().getId());
    }

    @Test
    public void reply() throws InterruptedException {
        AddVO addVO = new AddVO(T_Constant.FORMAT_JSON, "郭德纲《济公传》北展超清晰豪华全集！", "212.34.23.1");
        addVO.setLatitude(116.607214);
        addVO.setLongitude(40.087517);
        BackInfo backInfo = HttpURLConnectionUtil.postToObject(addVO);
        ResponseDetectUtil.detect(backInfo);

        Thread.sleep(15000);

        ReplyVO replyVO = new ReplyVO(T_Constant.FORMAT_JSON, "很好！", "212.34.23.1", backInfo.getData().getId());
        replyVO.setLatitude(116.607214);
        replyVO.setLongitude(40.087517);
        backInfo = HttpURLConnectionUtil.postToObject(replyVO);
        ResponseDetectUtil.detect(backInfo);
    }

    @Test
    public void comment() throws InterruptedException {
        AddVO addVO = new AddVO(T_Constant.FORMAT_JSON, "郭德纲《济公传》北展超清晰豪华全集！", "212.34.23.1");
        addVO.setLatitude(116.607214);
        addVO.setLongitude(40.087517);
        BackInfo backInfo = HttpURLConnectionUtil.postToObject(addVO);
        ResponseDetectUtil.detect(backInfo);

        Thread.sleep(15000);

        CommentVO commentVO = new CommentVO(T_Constant.FORMAT_JSON, "很逗！", "212.34.23.1", backInfo.getData().getId());
        commentVO.setLatitude(116.607214);
        commentVO.setLongitude(40.087517);
        backInfo = HttpURLConnectionUtil.postToObject(commentVO);
        ResponseDetectUtil.detect(backInfo);
    }

    @Test
    public void list() throws InterruptedException {
        AddVO addVO = new AddVO(T_Constant.FORMAT_JSON, "郭德纲《济公传》北展超清晰豪华全集！还不错。", "212.34.23.1");
        addVO.setLatitude(116.607214);
        addVO.setLongitude(40.087517);
        BackInfo backInfo = HttpURLConnectionUtil.postToObject(addVO);
        ResponseDetectUtil.detect(backInfo);

        Thread.sleep(15000);

        ListVO listVO = new ListVO(T_Constant.FORMAT_JSON, backInfo.getData().getId());
        System.out.println(HttpURLConnectionUtil.get(listVO));
        // FIXME
    }

    @Test
    public void reCount() throws InterruptedException {
        AddVO addVO = new AddVO(T_Constant.FORMAT_JSON, "郭德纲《济公传》北展超清晰豪华全集！!!!红油漆", "212.34.23.1");
        addVO.setLatitude(116.607214);
        addVO.setLongitude(40.087517);
        BackInfo backInfo = HttpURLConnectionUtil.postToObject(addVO);
        ResponseDetectUtil.detect(backInfo);

        Thread.sleep(15000);

        ReCountVO reCountVO = new ReCountVO(T_Constant.FORMAT_JSON, backInfo.getData().getId(), 2);
        System.out.println(HttpURLConnectionUtil.get(reCountVO));
    }

    @Test
    public void subReCount() {

    }

    @Test
    public void reList() {

    }

    @Test
    public void show() {

    }

    @Test
    public void getVideoInfo() {

    }
}
